package net.dgg.zz.production.distribution.controller;

import net.dgg.framework.tac.utils.bean.DggRestResponse;
import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.iboss.base.util.CustomerNumberEncryptionUtil;
import net.dgg.iboss.base.util.DesUtil;
import net.dgg.iboss.base.util.datatable.DataTableUtils;
import net.dgg.iboss.base.util.datatable.domain.DataTableRequest;
import net.dgg.iboss.base.util.datatable.domain.DataTableResponse;
import net.dgg.iboss.base.util.poi.ExcelUtil;
import net.dgg.tmd.foundation.platform.common.util.TranslateUtil;
import net.dgg.tmd.foundation.platform.datapermission.entity.DataAuthDTO;
import net.dgg.tmd.foundation.platform.datapermission.service.DataPermissionAssocService;
import net.dgg.zz.common.constant.ScProductConstant;
import net.dgg.zz.common.utils.OrderHandleUtils;
import net.dgg.zz.production.distribution.service.DistributionService;
import net.dgg.zz.production.entity.ProductOrder;
import net.dgg.zz.production.excption.ProductOrderExcption;
import net.dgg.zz.production.inProcess.entity.Assistant;
import net.dgg.zz.production.inProcess.service.ZzInProcessService;
import net.fblock.log.NLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 待分配订单
 *<p>@Package: net.dgg.zz.production.inProcess.controller</p>
 *<p>@Title：AllotScOrderController</p>
 *<p>@description:</p>
 *<p>@author liulei</p>
 *<p>@date 2018年8月14日</p>
 *<p>@Company dgg</p>
 */
@Controller
@RequestMapping("/sc/distribution")
public class DistributionController extends DggBaseController {

    @Autowired
    private DataPermissionAssocService dataPermissionAssocService;
    @Autowired
    private ZzInProcessService zzInProcessService;
    
    
    //新
    @Autowired
    private DistributionService  distributionservice;
    
    
    @Autowired
    private DesUtil desUtil;
    
    
    
    /**
     * json查询所有待分配列表
     * @param params
     * @param request
     * @returnSC_GENERAL_STATE_SCPROORDER_CODE1
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
	@NLog
    @RequestMapping(value = "/queryPage")
    @ResponseBody
    public DataTableResponse queryPage(@RequestParam Map params, HttpServletRequest request) {
    	   DataTableRequest dt = DataTableUtils.getParam(request);
           params.put("page", dt.getPage());
           params.put("limit", dt.getLength());
           Map fileMap = new HashMap();
           fileMap.put("orderTime", "orderTime");
           OrderHandleUtils.getOrderCol(dt, params, fileMap);
            // 数据权限
           DataAuthDTO dataAuthDTO = dataPermissionAssocService.selectAuthToConfigureRturnOrgIds("zz_zzsc_waitdistrubute");
           params.put("dataAuthDTO", dataAuthDTO);
           params.put("deptAlias", "t");
           params.put("deptColumn", "product_org_id");
           List<Map> list = distributionservice.findAllWithPage(params);
           String[] hideColum = {"customerPhone"};//隐藏
           list = desUtil.decryptListMap(hideColum, list);
           list = CustomerNumberEncryptionUtil.encryptionList(hideColum,list);
           int count = Integer.parseInt(params.get("count").toString());//总条数
           return new DataTableResponse(dt.getDraw(), count, count, list, "");
    }

    
    /**
     * 待分配订单列表
     * @param model
     * @param status 状态
     * @return
     */
    @NLog
    @RequestMapping(value = "/waitAllotScOrder")
    public String orderListPage(Model model,String status) {
        model.addAttribute("ALLOT_WAITING", ScProductConstant.SC_TO_RECEIVE_XDDJS);
        model.addAttribute("ALLOT_REFUSED", ScProductConstant.SC_ORDER_BACK_TO_ALLOT);
        model.addAttribute("ALL","all");
        if(net.dgg.zz.common.utils.StringUtils.isEmpty(status)) {
            model.addAttribute("status", "all");//默认全部待分配
        }
        else {
        	  model.addAttribute("status",status);
        }
        model.addAttribute("TIME_OUT", ScProductConstant.TIME_OUT);
        return "zzsc/distribution/waitAllotScOrder";
    }
    
    /**
     * 分配页面
     *
     * @param model
     * @param id 生产订单id
     * @param scProductIds
     * @return
     */
    @NLog
    @RequestMapping(value = "/allotScOrderPage")
    public String allotScOrderPage(Model model,Long id, String scProductIds) {
        model.addAttribute("ALLOT_TO_FLOW_USER", ScProductConstant.ALLOT_TO_FLOW_USER);
        model.addAttribute("ALLOT_TO_PRODUCT_USER", ScProductConstant.ALLOT_TO_PRODUCT_USER);
        model.addAttribute("ALLOT_TO_PRODUCT_DEPT", ScProductConstant.ALLOT_TO_PRODUCT_DEPT);
        model.addAttribute("productOrderInfo",distributionservice.selectByPrimaryKey(id));
        //获取协单人员数据
        StringBuilder ids = new StringBuilder();
        List<Assistant> scAssistantList = zzInProcessService.queyScAssistantList(id);
        if (scAssistantList != null && !scAssistantList.isEmpty()) {
            for (Assistant scAssistant : scAssistantList) {
                ids.append(scAssistant.getAssistUserId().toString().concat(","));
            }
            model.addAttribute("ids", ids.deleteCharAt(ids.lastIndexOf(",")));
        }
        return "zzsc/distribution/allotScOrderPage";
    }

    /**
     * 部门分配页面
     *
     * @param model
     * @param id 生产订单id
     * @param scProductIds
     * @return
     */
    @NLog
    @RequestMapping(value = "/deptAllotScOrderPage")
    public String deptAllotScOrderPage(Model model,Long id, String scProductIds) {
        ProductOrder productOrder = distributionservice.selectByPrimaryKey(id);
        model.addAttribute("ALLOT_TO_FLOW_USER", ScProductConstant.ALLOT_TO_FLOW_USER);
        model.addAttribute("ALLOT_TO_PRODUCT_USER", ScProductConstant.ALLOT_TO_PRODUCT_USER);
        model.addAttribute("ALLOT_TO_PRODUCT_DEPT", ScProductConstant.ALLOT_TO_PRODUCT_DEPT);
        model.addAttribute("allotWay", productOrder.getAllotWay());
        model.addAttribute("productOrderInfo",productOrder);
        //获取协单人员数据
        StringBuilder ids = new StringBuilder();
        List<Assistant> scAssistantList = zzInProcessService.queyScAssistantList(id);
        if (scAssistantList != null && !scAssistantList.isEmpty()) {
            for (Assistant scAssistant : scAssistantList) {
                ids.append(scAssistant.getAssistUserId().toString().concat(","));
            }
            model.addAttribute("ids", ids.deleteCharAt(ids.lastIndexOf(",")));
        }
        return "zzsc/distribution/deptAllotScOrderPage";
    }



    /**
     * 备注页面
     *
     * @param model
     * @param id 生产订单id
     * @return
     */
    @NLog
    @RequestMapping(value = "/remarkScOrderPage")
    public String returnScOrderPage(Model model,Long id) {
    	return "zzsc/distribution/remarkPage";
    }


    /**
     * 分配订单
     *
     * @return
     */
    @NLog
    @RequestMapping(value = "/allotScOrder", method = RequestMethod.POST)
    @ResponseBody
    public DggRestResponse allotScOrder(@RequestParam Map params) {
        String result = distributionservice.distributionProductOrder(params);
        if (result.equals("success")) {
            return this.getSuccessResponse("操作成功");
        } else {
            return this.getFailResponse(result);
        }
    }
    /**
     * 订单备注
     * @param params
     * @return
     */
    @NLog
    @ResponseBody
    @RequestMapping(value="/remarkOrder",method = RequestMethod.POST)
    public DggRestResponse remarkOrder(@RequestParam Map params) {
    	net.dgg.zz.common.utils.ValidateUtils.strNotEmpty(params.get("order_remark"),ProductOrderExcption.class,"请填写备注信息");
    	String result =	distributionservice.remarkOrder(params);
    	if(result.equals("success")) {
            return this.getSuccessResponse("操作成功");
        }
    	else {
            return this.getFailResponse(result);
        }
    }


    @RequestMapping(value = "/export", method = RequestMethod.POST)
    @ResponseBody
    public void POIExcel(@RequestParam Map params, HttpServletRequest request, HttpServletResponse response) {
        // 查询数据集合
        // 数据权限
        DataAuthDTO dataAuthDTO = dataPermissionAssocService.selectAuthToConfigureRturnOrgIds("zz_zzsc_waitdistrubute");
        params.put("dataAuthDTO", dataAuthDTO);
        params.put("deptAlias", "t");
        params.put("deptColumn", "product_org_id");

    	List<Map>list = distributionservice.findAllWithPage(params);
        String[] hideColum = {"customerPhone"};//隐藏
        list = desUtil.decryptListMap(hideColum, list);
        list = CustomerNumberEncryptionUtil.encryptionList(hideColum,list);
        String fileName = "待分配" + ".xls";
        String[] title = {"生产事业部", "订单编号", "生产订单编号", "合同编号","客户","客户号码","业务类型", "订单产品", "下单时间", "签单商务","签单部门","订单来源","合同金额"};
        String[] key = {"productOrgName", "orderNo", "scProductOrderNo", "contractNo","customerName", "customerPhone", "businessProductName", "productName", "orderTime","signUserName","signUserOrgName","signOrgName","money"};
        ExcelUtil.createN(fileName, title, key, list, response);
    }

    /*
     * @Author zsx
     * @Date 2018-09-28 10:09:08
     * @Description 工作台跳转待分配订单列表
     * @param null
     * @return String
     */
    @RequestMapping("/goToWaitAllotOrder")
    public String goToWaitAllotOrder(Model model){
        model.addAttribute("ALLOT_WAITING", ScProductConstant.SC_TO_RECEIVE_XDDJS);
        model.addAttribute("ALLOT_REFUSED", ScProductConstant.SC_ORDER_BACK_TO_ALLOT);
        model.addAttribute("ALL","all");
        model.addAttribute("status","all");
        model.addAttribute("TIME_OUT", ScProductConstant.TIME_OUT);
        return "zzsc/distribution/waitAllotScOrder";
    }

    /*
     * @Author zsx
     * @Date 2018-09-28 10:09:53
     * @Description 工作台跳转退回待分配列表
     * @param null
     * @return String
     */
    @RequestMapping("/goToReturnWaitAllotOrder")
    public String goToReturnWaitAllotOrder(Model model){
        model.addAttribute("ALLOT_WAITING", ScProductConstant.SC_TO_RECEIVE_XDDJS);
        model.addAttribute("ALLOT_REFUSED", ScProductConstant.SC_ORDER_BACK_TO_ALLOT);
        model.addAttribute("ALL","all");
        model.addAttribute("status",ScProductConstant.SC_ORDER_BACK_TO_ALLOT);
        model.addAttribute("TIME_OUT", ScProductConstant.TIME_OUT);
        return "zzsc/distribution/waitAllotScOrder";
    }


    /*
     * @Author zsx
     * @Date 2018-09-28 10:09:26
     * @Description 工作台跳转分配超期订单
     * @param null
     * @return String
     */
    @RequestMapping("/goToAllotTimeOutOrder")
    public String goToAllotTimeOutOrder(Model model){
        model.addAttribute("ALLOT_WAITING", ScProductConstant.SC_TO_RECEIVE_XDDJS);
        model.addAttribute("ALLOT_REFUSED", ScProductConstant.SC_ORDER_BACK_TO_ALLOT);
        model.addAttribute("ALL","all");
        model.addAttribute("status","all");
        model.addAttribute("TIME_OUT", ScProductConstant.TIME_OUT);
        model.addAttribute("timeOutStatus",ScProductConstant.TIME_OUT);
        return "zzsc/distribution/waitAllotScOrder";
    }



    /**
     * <p>@Description 批量分配页面</p>
     * <p>@author mqy</p>
     * <p>@Time 2018/11/12 0012 15:35 创建时间</p>
     * <p>@return</p>
     */
    @NLog
    @RequestMapping(value = "/allot_order_list_page")
    public String allotOrderListPage(Model model,Long productOrgId) {
        model.addAttribute("productOrgId", productOrgId);
        model.addAttribute("ALLOT_TO_FLOW_USER", ScProductConstant.ALLOT_TO_FLOW_USER);
        model.addAttribute("ALLOT_TO_PRODUCT_USER", ScProductConstant.ALLOT_TO_PRODUCT_USER);
        model.addAttribute("ALLOT_TO_PRODUCT_DEPT", ScProductConstant.ALLOT_TO_PRODUCT_DEPT);
        return "zzsc/distribution/allot_order_list_page";
    }
    /**
     * <p>@Description 批量分配方法</p>
     * <p>@author mqy</p>
     * <p>@Time 2018/11/12 0012 15:43 创建时间</p>
     * <p>@return</p>
     */
    @NLog
    @RequestMapping(value = "/allot_order_list", method = RequestMethod.POST)
    @ResponseBody
    public DggRestResponse allotScOrderList(@RequestParam Map params) {
        String result = distributionservice.distributionProductOrderList(params);
        if (result.equals("success")) {
            return this.getSuccessResponse("操作成功");
        } else {
            return this.getFailResponse(result);
        }
    }
}